//
//  YZXStatusCell.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/23.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

import SnapKit

class YZXStatusCell: UITableViewCell {
    
 /// 记录底部view的约束
    var bottomCon:Constraint?
    
    // 当cell有模型的时候给
    var status:YZXStatus?{
        didSet{
            originaView.status = status
            // 给被转发微博赋值
            retweetView.retweetStatus = status?.retweeted_status
            
            // 在更新约束之前需要先清空之前的约束  因为是要更改约束的参照
            bottomCon?.uninstall()
            
            // 如果没有被转发的微博 那么就隐藏视图
            if status?.retweeted_status == nil {
                retweetView.hidden = true
                // 更新底部view的顶部约束
                buttomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCon = make.top.equalTo(originaView.snp_bottom).constraint
                })
            } else {
                
                retweetView.hidden = false
                // 存在被转发的微博
                // 更新底部view的约束
                buttomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCon = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
            }
            
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 设置orgoinaView
        self.setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI(){
        self.contentView.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
        
        // 添加到父控件
        self.contentView.addSubview(originaView)
        self.contentView.addSubview(retweetView)
        self.contentView.addSubview(buttomView)
        
        
        // 添加约束
        originaView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(8)
            
            // 模拟原创view不固定的高度
            //            let height = CGFloat(arc4random_uniform(60)) + 44
            //            make.height.equalTo(height)
        }
        
        // 被转发的微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(originaView.snp_bottom)
        }
        
        
        // 底部的视图
        buttomView.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self.contentView)
            // 记录底部view的约束
            bottomCon = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(bottomHeight)
        }
        
        
        
        
        // 让cell的高度自己适应,其实是根据conetntView的高度来
        // 需要约束好contentView
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
        }
        
        // contentView的高度约束需要参照里面的内容来
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(buttomView)
        }
        
        
    }
    /// 自己发的微博的内容
    lazy var originaView:YZXOriginaView = YZXOriginaView()
    /// 底部转发、评论的按钮
    lazy var buttomView:YZXButtomView = YZXButtomView()
    
    // 被转发微博的view
    private lazy var retweetView:YZXRetweetView = YZXRetweetView()

}
